import { _decorator, Component, Label, math, Node, Vec3 } from 'cc';
import { UIBase } from '../utils/UIFramework/UIBase';
import { ObjectPool } from '../utils/ObjectPool/ObjectPool';
import { StarEntity, StarType } from '../Entity/StarEntity';
import UIManager from '../utils/UIFramework/UIManager';
const { ccclass, property } = _decorator;

@ccclass('GameUI')
export class GameUI extends UIBase {

    public static readonly MAX_ROW = 8;

    public static readonly MAX_COL = 8;

    public static readonly STAR_L = 70;

    private starObjectPool : ObjectPool<StarEntity> = new ObjectPool(StarEntity.Create);
    
    private allStars : Array<Array<StarEntity>> = [];

    private selectCount : number = 0;

    private point : number = 0;
    
    public set Point(v : number) {
        this.point = v;
        this.uiMap.GetComponent<Label>("pointText",Label.name).string = "得分：" + this.point.toString();
    }
    
    public get Point() {
        return this.point;
    }

    public get Star() : Node {
        return this.uiMap.GetNode("stars");
    }
    

    public async onOpen() {
        await this._initStar();
        this.Point = 0;
    }

    public onClose(): void {
        
    }

    private async _initStar()
    {
        for (let i = 0; i < GameUI.MAX_COL; i++) {
            let col = [];
            for (let j = 0; j < GameUI.MAX_ROW; j++) {
                let star = await this._createStart();
                col.push(star);
            }
            this.allStars.push(col);
        }
        
        this._updateStar(this);
        
    }

    private async _createStart(){
        //  生成随机整数
        let type = this._getRandomNum(0,4);
        let star = await this.starObjectPool.LentItem(type,this.Star);
        star.StarTouched.AddFunc(this._onStarTouch,this);
        return star;
    }

    private _getRandomNum(min : number,max : number)
    {
        return Math.floor(Math.random()*(max-min+1)+min);
    }

    private async _updateStar(self : GameUI)
    {
        for (let i = 0; i<self.allStars.length; i++) {
            for (let j = 0; j<self.allStars[i].length; j++) {
                self.allStars[i][j].row = j;
                self.allStars[i][j].col = i;
                let x = i * GameUI.STAR_L;
                let y = j * GameUI.STAR_L;
                self.allStars[i][j].node.setPosition(new Vec3(x,y,0));
                await self.allStars[i][j].UpdateStar();
            }
        }
    }

    private _onStarTouch(self:GameUI, ...param:any)
    {
        self.selectCount = 0;

        let data : StarEntity = param[0][0];
        let type : StarType = param[0][1];

        self._selectStars(self,data,type);

        if(self.selectCount < 2){
            return;
        }

        self.Point = self.Point + self.selectCount;

        for (let i = 0; i < self.allStars.length; i++) {
            let listSelect = self.allStars[i].filter(item => item.IsSelected);
            let listNoSelect  = self.allStars[i].filter(item => !item.IsSelected);
            for (const item of listSelect) {
                item.CloseSelf();
            }
            self.allStars[i] = listNoSelect;
        }

        //  去除所有空的列
        self.allStars = self.allStars.filter(col => !(col.length == 0));
        console.log(self.allStars);
        self._updateStar(self);
    }

    private _selectStars(self:GameUI, ...param:any[])
    {
        let data : StarEntity = param[0];
        let type : StarType = param[1];

        if(!data){
            return;
        }

        // 防止往回走,如果遇到不同类型也不需要往这走了
        if(data.IsSelected || data.Type != type)
        {
            return;
        }

        data.IsSelected = true;
        self.selectCount ++;

        //  下
        if(data.row > 0)
        {
            self._selectStars(self,self.allStars[data.col][data.row - 1],type);
        }
        //  上
        if(data.row < self.allStars[data.col].length - 1)
        {
            self._selectStars(self,self.allStars[data.col][data.row + 1],type);
        }
        //  左
        if(data.col > 0){
            if(data.row < self.allStars[data.col - 1].length)
            {
                self._selectStars(self,self.allStars[data.col - 1][data.row],type);
            }

        }
        //  右
        if(data.col < self.allStars.length - 1)
        {
            //  防止尖端报空
            if(data.row < self.allStars[data.col + 1].length){
                self._selectStars(self,self.allStars[data.col + 1][data.row],type);
            }
        }

    }
}


